package com.example.spring_boot_demo.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.example.spring_boot_demo.dto.Goods;
import com.example.spring_boot_demo.dto.GoodsRequest;
import com.example.spring_boot_demo.mapper.GoodsMapper;
import com.example.spring_boot_demo.service.GoodsService;
import com.example.spring_boot_demo.utils.AjaxResult;
import com.example.spring_boot_demo.utils.PageUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <P> @Author: SongKeCheng
 * <P> Type: Class
 * <P> Description:
 * <P> Date: 2024/5/29:19:48
 */

@Service
@SuppressWarnings({"unchecked", ""})
public class GoodsServiceImpl implements GoodsService {
    private static final Logger log = LoggerFactory.getLogger(GoodsServiceImpl.class);

    @Resource
    private GoodsMapper goodsMapper;


    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @Override
    public AjaxResult findGoods(GoodsRequest goodsRequest) {
        final String goodsKey = "list_key";

        Object redisData = redisTemplate.opsForValue().get(goodsKey);
        PageUtils pageUtils;
        if (!ObjectUtils.isEmpty(redisData)) {
            pageUtils = getData(new TypeReference<PageUtils>(){},redisData);
            return AjaxResult.success(pageUtils);
        }
        PageHelper.startPage(goodsRequest.getPageNum(), goodsRequest.getPageSize());
        List<Goods> goodsList = goodsMapper.findGoods(goodsRequest);
        PageInfo<Goods> pageAll = PageInfo.of(goodsList);
        pageUtils = new PageUtils(pageAll);
        redisTemplate.opsForValue().set(goodsKey, pageUtils,60, TimeUnit.SECONDS);
        return AjaxResult.success(pageUtils);
    }



    /**
     * 根据传入的类型 把封装的 data 数据获取并且转为这个类型
     *
     * @param typeReference 指定返回类型
     * @return T
     **/
    public <T> T getData(TypeReference<T> typeReference, Object data) {
        String jsonString = JSON.toJSONString(data);
         return JSON.parseObject(jsonString,typeReference );
    }


    @Override
    public AjaxResult deleteGoods(GoodsRequest goodsRequest) {
        int i = goodsMapper.deleteByPrimaryKey(goodsRequest.getId());
        if (i > 0) {
            return AjaxResult.success("删除成功");
        } else {
            return AjaxResult.error("删除失败,可能不存在此条记录");
        }
    }

    @Override
    public AjaxResult updateGoods(GoodsRequest goodsRequest) {
        if (ObjectUtils.isEmpty(goodsRequest.getId())) {
            throw new NullPointerException("更新操作 主键id不能为空");
        }
        Goods goods = new Goods();
        BeanUtils.copyProperties(goodsRequest, goods);
        int i = goodsMapper.updateByPrimaryKeySelective(goods);
        if (i > 0) {
            return AjaxResult.success("更新成功");
        } else {
            return AjaxResult.error("更新失败");
        }
    }

    @Override
    public AjaxResult addGoods(GoodsRequest goodsRequest) {
        Goods goods = new Goods();
        BeanUtils.copyProperties(goodsRequest, goods);
        //checkName();
        int i = goodsMapper.insertSelective(goods);
        if (i > 0) {
            return AjaxResult.success("新增成功");
        } else {
            return AjaxResult.error("新增失败");
        }
    }

    @Transactional(isolation = Isolation.DEFAULT)
    @Override
    public AjaxResult testTransactional(GoodsRequest goodsRequest) {
        Goods goods = new Goods();
        BeanUtils.copyProperties(goodsRequest, goods);
        goodsMapper.insert(goods);
        int i = 1 / 0;
        goodsMapper.updateByPrimaryKey(goods);
        return AjaxResult.error("操作成功");
    }
}
